<#import "/manage/tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="商品属性">
<form id="searchForm" class="form-panel">
    <ul class="panel-content row">
        <li>
            <div class="form-actions span8">
                <a href="javascript:;" class="button button-primary">
                    <i class="icon-plus-sign icon-white"></i> 查询
                </a>
                <a href="javascript:add()" class="button button-success">
                    <i class="icon-plus-sign icon-white"></i> 添加
                </a>
                <a class="button button-danger" onclick="return delFunction();">
                    <i class="icon-remove-sign icon-white"></i> 删除
                </a>
            </div>
        </li>
    </ul>
</form>
<div id="addContent" style="display: none">
    <form action="${basepath}/manage/attribute/insertJson" id="addForm" method="post" class="form-horizontal">
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">分类</label>
                <div class="controls">
                    <select data-rules="{required:true}" name="pid">
                        <option value="">-请选择-</option>
                        <option value="0">商品属性</option>
                        <option value="-1">商品参数</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">名称：</label>
                <div class="controls">
                    <input type="text" class="control-text input-normal" name="name"/>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">所属类别：</label>
                <div class="controls">
                    <input type="text" class="control-text input-normal" name="catalogName"/>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">顺序：</label>

                <div class="controls">
                    <input type="text" class="control-text input-normal" name="order1"/>
                </div>
            </div>
        </div>
    </form>
</div>
<div id="editContent" style="display: none">
    <form action="${basepath}/manage/attribute/insertJson" id="editForm" method="post" class="form-horizontal">
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">分类</label>
                <div class="controls">
                    <select data-rules="{required:true}" name="pid">
                        <option value="">-请选择-</option>
                        <option value="0">商品属性</option>
                        <option value="-1">商品参数</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">名称：</label>
                <div class="controls">
                    <input type="text" class="control-text input-normal" name="name"/>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">所属类别：</label>
                <div class="controls">
                    <input type="text" class="control-text input-normal" name="catalogName"/>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">顺序：</label>

                <div class="controls">
                    <input type="text" class="control-text input-normal" name="order1"/>
                </div>
            </div>
        </div>
    </form>
</div>
<div id="grid"></div>
<script>
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title: '编号', dataIndex: 'id', width: 80},
                {
                    title: '分类', dataIndex: 'pid', width: 80, renderer: function (value) {
                    if (value == 0) {
                        return "商品属性";
                    }
                    return "商品参数";
                }
                },
                {title: '名称', dataIndex: 'name', width: 80},
                {title: '所属类别', dataIndex: 'catalogName', width: 80},
                {title: '顺序', dataIndex: 'order1', width: 80},
                {
                    title: '操作', dataIndex: 'id', width: 200, renderer: function (value) {
                    <#if checkPrivilege("/manage/user/edit")>
                        return '<a href="javascript:edit(' + value + ')">编辑</a>';
                    <#else>
                        return "";
                    </#if>
                }
                }
            ];
    var store = new Store({
                url: 'loadData',
                autoLoad: true, //自动加载数据
                params: { //配置初始请求的参数
                    length: '10'
                },
                pageSize: 10,	// 配置分页数目
                root: 'list',
                totalProperty: 'total'
            }),
            grid = new Grid.Grid({
                render: '#grid',
                columns: columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格

                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            });
    grid.render();

    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
    /*----------------------------------------------新增表单处理 begin------------------------------------*/
    var addForm = new BUI.Form.Form({
        srcNode: '#addForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            addDialog.close();
        }
    }).render();

    var addDialog = new BUI.Overlay.Dialog({
        title: '新增商品属性',
        width: 500,
        height: 380,
        contentId: 'addContent',
        success: function () {
            if (addForm.isValid()) {
                addForm.ajaxSubmit();
            } else {
                addForm.valid();
            }
        }
    });
    //添加按钮事件
    function add() {
        addDialog.show();
    }
    /*----------------------------------------------新增表单处理 end------------------------------------*/
    /*----------------------------------------------编辑表单处理 begin------------------------------------*/
    var editForm = new BUI.Form.Form({
        srcNode: '#editForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            editDialog.close();
        }
    });
    editForm.render();

    var editDialog = new BUI.Overlay.Dialog({
        title: '编辑商品属性',
        width: 500,
        height: 320,
        contentId: 'editContent',
        success: function () {
            editForm.ajaxSubmit();
        }
    });
    //编辑按钮事件
    function edit(id) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "${basepath}/manage/attribute/toEditJson",
            data: {id: id},
            success: function (data) {
                var form = $("#editForm")[0];
                BUI.FormHelper.setFields(form, data.data);
                editDialog.show();
            }
        });
    }
    /*----------------------------------------------编辑表单处理 end------------------------------------*/

    //删除选中的记录
    function delFunction() {
        var selections = grid.getSelection();
        var ids = new Array();
        for (var i = 0; i < selections.length; i++) {
            ids[i] = selections[i].id.toString()
        }
        $.ajax({
            type: "POST",
            url: "${basepath}/manage/attribute/deletesJson",
            dataType: "json",
            data: {
                ids: ids
            },
            success: function (data) {
                var obj = new Object();
                obj.start = 0; //返回第一页
                store.load(obj);
            }
        });

    }
</script>
</@page.pageBase>